package com.sunshine.lnuplus.adapter

import android.content.Context
import android.content.DialogInterface
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.cardview.widget.CardView
import androidx.fragment.app.FragmentManager
import androidx.recyclerview.widget.RecyclerView
import com.sunshine.lnuplus.R
import com.sunshine.lnuplus.model.InitData
import com.sunshine.lnuplus.view.DialogWithoutView
import com.sunshine.lnuplus.view.ScrollTextView
import com.sunshine.lnuplus.view.ShortCut
import java.lang.Exception

/**
 * Created by sunshine on 2020/3/16
 */

class ShortCutRecyclerAdapter(
    private val context: Context,
    private val color: Int,
    private val fm: FragmentManager) : RecyclerView.Adapter<ShortCutRecyclerAdapter.ViewHolder>() {

    private val functionList: ArrayList<ArrayList<String>> = ArrayList()
    private val addedFunctionList = ArrayList<ArrayList<String>>()
    private val appSetting = context.getSharedPreferences("com.sunshine.lnuplus_preferences",Context.MODE_PRIVATE)

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
        val name: ScrollTextView = itemView.findViewById(R.id.shortcut_scrollTextView)
        val button: Button = itemView.findViewById(R.id.shortcut_button)
        val card: CardView = itemView.findViewById(R.id.shortcut_card)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_shortcut, parent, false))
    }

    override fun getItemCount(): Int {
        return functionList.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.name.text = functionList[position][0]
        holder.card.setCardBackgroundColor(color)
        if(addedFunctionList.contains(functionList[position])){
            holder.button.text = "移除"
        }
        holder.button.setOnClickListener {
            if(addedFunctionList.contains(functionList[position])){
                holder.button.text = "添加"
                addedFunctionList.remove(functionList[position])
            } else {
                if(addedFunctionList.size > 3){
                    DialogWithoutView.newInstance(
                        "提示",
                        "Shortcut最多只能添加四个功能，请移除一些后再试",
                        false,
                        DialogInterface.OnClickListener { _, _ -> },
                        null).show(fm, "shortcut_most")
                } else {
                    holder.button.text = "移除"
                    addedFunctionList.add(functionList[position])
                }
            }
            val edit = appSetting.edit()
            var functionString = ""
            addedFunctionList.forEach{
                functionString += "${it[0]}#${it[1]}*"
            }
            edit.putString("short_cut", functionString)
            edit.apply()
            ShortCut.init(context)
        }
    }

    init {
        val addedFunction = appSetting.getString("short_cut", "成绩查询#6")

        functionList.apply {
            add(arrayListOf("软件设置","1"))
            add(arrayListOf("课表设置","2"))
            add(arrayListOf("常见问题","3"))
            add(arrayListOf("反馈","4"))
            add(arrayListOf("关于","5"))
            add(arrayListOf("成绩查询","6"))
            add(arrayListOf("校历","7"))
            add(arrayListOf("导出到日历","8"))
        }
        InitData.functionList?.forEach {
            functionList.add(it)
        }

        addedFunction?.split("*")?.forEach {
            try {
                val arrayList = arrayListOf(it.split("#")[0], it.split("#")[1])
                if(!functionList.contains(arrayList)){
                    DialogWithoutView.newInstance(
                        "提示",
                        "“${arrayList[0]}”功能已经从列表中移除，因为现在它不在功能列表中",
                        false,
                        DialogInterface.OnClickListener { _, _ -> },
                        null).show(fm, "shortcut")
                } else {
                    addedFunctionList.add(arrayList)
                }
            }catch (e: Exception){}
        }
    }
}